/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.isystem.autpart.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.isystem.autpart.beans.TSucursal;
import org.isystem.autpart.dao.DAOGeneral;
import org.isystem.autpart.util.idNew;
import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.Validator;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.bind.validator.AbstractValidator;

/**
 *
 * @author miguel-lap
 */
public class ManagerAdmSucursal {
    
    private static final long serialVersionUID = 1L;
    private List<TSucursal> sucursales;
    private TSucursal sucursal = new TSucursal();
    private TSucursal sucursalSelected = new TSucursal();
    private DAOGeneral dao = new DAOGeneral();
    private boolean verSave;
    private boolean mostrarAdd;
    private String saveLabel;
    private String buscar;
    
    //validators
    
    //validation messages
    Map<String, String> validationMessages = new HashMap<String,String>();
    
    @Init
    public void init(){
        System.out.println("Inicia AdminSucursal");
        actualizaListaSucursales("");
        mostrarAdd = false;
    }
    
    //Validators
    public Validator getFormValidator(){
        System.out.println("validator all");
        return new AbstractValidator() {
//            private HashMap validationMessages;

            public void validate(ValidationContext ctx) {
// Consultar si es persona natural o juridica
                String val = (String)ctx.getProperties("denominacion")[0].getValue();
                System.out.println("denominacion "  + val);
//                ValidationMessages vmsgs = (ValidationMessages) ctx.getValidatorArg("vmsgs");
//                validationMessages.put("usuarioTextBox", "value1 error");
//                ctx.setInvalid(); // mark invalid
//                ValidationMessages vmsgs = ((BinderCtrl)ctx.getBindContext().getBinder()).getValidationMessages();
//                addInvalidMessage(ctx, "usuario", "value1 error");
//                System.out.println("111");
//                ctx.getBindContext().getBinder().notifyChange(validationMessages, "usuarioTextBox");
//                if(invalidCondition01(val)){
//                    addInvalidMessage(ctx, "fkey1", "value1 error");
//                }
//                val = (String)ctx.getProperties("value2")[0].getValue();
//                if(invalidCondition02(val)){
//                    addInvalidMessage(ctx, "fkey2", "value2 error");
//                }
//                val = (String)ctx.getProperties("value3")[0].getValue();
//                if(invalidCondition03(val)){
//                    addInvalidMessage(ctx, "fkey3", "value3 error");
//                }
//                ctx.getBindContext().getBinder().notifyChange(validationMessages, "usuarioTextBox");
                
                
            }
        };
    }
    
    // botones
    @Command
    @NotifyChange({"verSave","sucursal","mostrarAdd","saveLabel"})
    public void onClickadd() {
        System.out.println("click en agregar");
        sucursal = new TSucursal();
        sucursal.setEstado("A");
        mostrarAdd = true;
        verSave = true;
        saveLabel ="Guardar";
    }
    
    @Command
    @NotifyChange({"sucursales", "buscar"})
    public void onClickSearch() {
        System.out.println("click search");
        actualizaListaSucursales(" where s.denominacion like '%"+buscar+"%'");
    }
    
    @NotifyChange({"sucursal","verSave","sucursales"})
    @Command()
    public void guardar() {
        System.out.println("guardar");
        //Asignacion de IDs
        if (saveLabel.equals("Guardar")) {
            //Asignacion de IDs
            sucursal.setId(idNew.id());
        } else {
            
        }
        dao.persist(sucursal);
        verSave = false;
        actualizaListaSucursales("");
        System.out.println("fin guarda");
        return;
    }
    
    @NotifyChange({"verSave","sucursal","mostrarAdd","saveLabel"})
    @Command()
    public void edita(@BindingParam("sucur") TSucursal su ) {
        System.out.println("editar");
        verSave = true;
        sucursal = new TSucursal();   
        sucursal = (TSucursal) dao.load(TSucursal.class, su.getId()+"");
        mostrarAdd = true;
        saveLabel = "Modificar";
        
    }
    
    @NotifyChange({"sucursal","mostrarAdd","saveLabel","sucursales"})
    @Command()
    public void elimina(@BindingParam("sucur") TSucursal su ) {
        System.out.println("eliminar");
        sucursal = su;
        dao.delete(sucursal);
        mostrarAdd = false;
        actualizaListaSucursales("");
    }
    
    // Change Selected
    @NotifyChange({"sucursal","verSave","mostrarAdd"})
    @Command()
    public void selecciona() {
        System.out.println("Selecciona Categoria");
        verSave = false;
        sucursal = new TSucursal();
        sucursal = sucursalSelected;
        mostrarAdd = true;
    }
    
    
    @Command()
    public void selecCatPadre() {
        System.out.println("Selecciona Categoria padre");
    }

    
    // Metodos
    private void actualizaListaSucursales(String sql) {
        sucursales = new LinkedList<TSucursal>();
        sucursales.clear();
        sucursales = dao.findAll("from TSucursal s" + sql);
    }

    public List<TSucursal> getSucursales() {
        return sucursales;
    }

    public void setSucursales(List<TSucursal> sucursales) {
        this.sucursales = sucursales;
    }

    public TSucursal getSucursal() {
        return sucursal;
    }

    public void setSucursal(TSucursal sucursal) {
        this.sucursal = sucursal;
    }

    public TSucursal getSucursalSelected() {
        return sucursalSelected;
    }

    public void setSucursalSelected(TSucursal sucursalSelected) {
        this.sucursalSelected = sucursalSelected;
    }

    public boolean isVerSave() {
        return verSave;
    }

    public void setVerSave(boolean verSave) {
        this.verSave = verSave;
    }

    public boolean isMostrarAdd() {
        return mostrarAdd;
    }

    public void setMostrarAdd(boolean mostrarAdd) {
        this.mostrarAdd = mostrarAdd;
    }

    public String getSaveLabel() {
        return saveLabel;
    }

    public void setSaveLabel(String saveLabel) {
        this.saveLabel = saveLabel;
    }

    public String getBuscar() {
        return buscar;
    }

    public void setBuscar(String buscar) {
        this.buscar = buscar;
    }

    public Map<String, String> getValidationMessages() {
        return validationMessages;
    }

    public void setValidationMessages(Map<String, String> validationMessages) {
        this.validationMessages = validationMessages;
    }

}
